"""基类
封装一些方法（地址，点击，输入，切换窗口，进入iframe框架，弹窗，滑动，）
"""
import time

from selenium .webdriver.remote.webdriver import WebDriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec


class Base():
    def __init__(self,driver :WebDriver):
        self.driver=driver
    def open_url(self,url):
        self.driver.get(url)
    def find_element(self,loc):
        try:

            return WebDriverWait(self.driver,20).until(ec.presence_of_element_located(loc))
            # WebDriverWait(self.driver,20,0.5).until(lambda driver:driver.find_element(*loc).is_displayed())
            # return self.driver.find_element(*loc)
        except Exception as e:
            str = time.strftime("%Y%m%d%H%M%S", time.localtime())
            self.driver.save_screenshot(str+'.img')
            print(loc)
            print(e)

    #点击
    def click(self,loc):
        self.find_element(loc).click()
    #输入
    def send_key(self,loc,value):
        self.find_element(loc).send_keys(value)
    #获取文本属性值
    def get_text(self,loc):
        return self.find_element(loc).text
    #进入iframe框架
    def into_inframe(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))
        # self.driver.switch_to.frame(loc)
    #退出iframe框架
    def out_iframe(self):
        self.driver.switch_to.default_content()
    #切换新窗口
    def switch_to(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    #滑动
    def scroll_x(self,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')
    #滑动到底部
    def scrool_buttom(self,loc):
        self.driver.switch_to.window(f'window.scrollTo(0,doument.scrollHeight)')
    #弹窗
    def alter(self):
        self.driver.switch_to.alert.accept()
    #下拉框的选择
    def select_get_value(self,loc,value):
        Select(self.find_element(loc)).select_by_value(value)
    def selecct_get_text(self,loc,value):
        Select(self.find_element(loc)).select_by_visible_text(value)
    def select_get_index(self,loc,value):
        Select(self.find_element(loc)).select_by_index(value)
    #悬停
    def move_to_element(self,loc):
        ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()


